/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import BD.Retenciones;
import Seniat.Seniat;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.File;

/**
 *
 * @author ARMGARCES
 */
public class VentanaArchivo extends javax.swing.JFrame {
    private File file;
    private static Ventana ventana;
    private String mensj;
    private int ancho  = 605;
    private int alto   = 183;
    /**
     * Creates new form VentanaArchivo
     * @param vent 
     */
    public VentanaArchivo(Ventana vent) {
        initComponents();
        this.setTitle("Seleccion de archivo");
        mensj = "";
        ventana = vent;
        l_archivo1.setText(mensj);
        jProgressBar1.setStringPainted(true);
    }

    /**
     * 
     */
    public void centrarVentana(){
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension screenSize = toolkit.getScreenSize();
        this.setSize(ancho,alto);

        int x = (screenSize.width  -  this.getWidth()) / 2;
        int y = (screenSize.height - this.getHeight()) / 2;
        this.setLocation(x, y);
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        l_archivo = new javax.swing.JLabel();
        jProgressBar1 = new javax.swing.JProgressBar();
        b_procesar = new javax.swing.JButton();
        b_borrar = new javax.swing.JButton();
        b_abrir = new javax.swing.JButton();
        l_archivo1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), "Seniat"));
        jPanel1.setMaximumSize(null);

        jLabel1.setText("Seleccione el archivo a ser procesado:");

        b_procesar.setText("Procesar Archivo");
        b_procesar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_procesarActionPerformed(evt);
            }
        });

        b_borrar.setText("Borrar Archivo");
        b_borrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_borrarActionPerformed(evt);
            }
        });

        b_abrir.setText("Abrir Archivo...");
        b_abrir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_abrirActionPerformed(evt);
            }
        });

        l_archivo1.setText("jLabel2");

        jButton1.setText("Regresar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(l_archivo1))
                            .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 550, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(l_archivo)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(b_abrir)
                        .addGap(18, 18, 18)
                        .addComponent(b_borrar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(b_procesar)
                        .addGap(23, 23, 23))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(l_archivo)
                    .addComponent(l_archivo1))
                .addGap(18, 18, 18)
                .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(b_procesar)
                    .addComponent(b_borrar)
                    .addComponent(b_abrir)
                    .addComponent(jButton1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void b_abrirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_abrirActionPerformed
        // TODO add your handling code here:
        //
        FileChooser fileChooser = new FileChooser(this,true);
        fileChooser.setTitle("Seleccionar archivo a ser Procesado");
        fileChooser.centrarVentana();
        fileChooser.setVisible( true );
        fileChooser.setPreferredSize( new Dimension(100, 100) );

    }//GEN-LAST:event_b_abrirActionPerformed

    private void b_borrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_borrarActionPerformed
        // TODO add your handling code here:
        //
        file = null;
        mensj = "";
        l_archivo1.setText(mensj);
        l_archivo1.repaint();
        
    }//GEN-LAST:event_b_borrarActionPerformed

    private void b_procesarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_procesarActionPerformed
        // TODO add your handling code here:
        //
        Retenciones.borrarRegistrosDelMes(file);
        Seniat.getInstance().procesarArchivo(file);
    }//GEN-LAST:event_b_procesarActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        //
        ventana.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton b_abrir;
    private javax.swing.JButton b_borrar;
    private javax.swing.JButton b_procesar;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JProgressBar jProgressBar1;
    private javax.swing.JLabel l_archivo;
    private javax.swing.JLabel l_archivo1;
    // End of variables declaration//GEN-END:variables

    /**
     * 
     * @param _file 
     */
    protected void updateFile(File _file) {
        file = _file;
        
        if(file != null){
            mensj = file.getName();
        }else{
            mensj = "";
        }
        l_archivo1.setText(mensj);
        
        l_archivo1.repaint();
    }

    /**
     * @param begin
     * @param end  
     */
    public void iniciarProgressBar(int begin,int end) {
        jProgressBar1.setMinimum(begin);
        jProgressBar1.setMaximum(end);
    }
    /**
     * 
     * @param value 
     */
    public void updateProgressBar(int value){
        jProgressBar1.setValue(value);
        jProgressBar1.update(jProgressBar1.getGraphics());
    }
}
